home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / gcl-1.0-l / gcl-1 / usr / local / lib / gcl-1.0 / doc / sshell.el < prev   
Encoding:
Text File  |  1994-05-09  |  11.9 KB  |  347 lines

  1. ;; Run subshell under Emacs
  2. ;; Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. ;; The following is a "simple shell" much like the one in version 18
  21. ;; of emacs.   Unfortunately cmint breaks most code which tries to use
  22. ;; the shell mode, and is rather complex.   
  23.  
  24. (provide 'sshell)
  25.  
  26.  
  27. (defvar last-input-start nil
  28.   "In a shell-mode buffer, marker for start of last unit of input.")
  29. (defvar last-input-end nil
  30.   "In a shell-mode buffer, marker for end of last unit of input.")
  31.  
  32. (defvar shell-mode-map nil)
  33.  
  34. (defvar shell-directory-stack nil
  35.   "List of directories saved by pushd in this buffer's shell.")
  36.  
  37. (defvar shell-popd-regexp "popd"
  38.   "*Regexp to match subshell commands equivalent to popd.")
  39.  
  40. (defvar shell-pushd-regexp "pushd"
  41.   "*Regexp to match subshell commands equivalent to pushd.")
  42.  
  43. (defvar shell-cd-regexp "cd"
  44.   "*Regexp to match subshell commands equivalent to cd.")
  45.  
  46. (defvar explicit-shell-file-name nil
  47.   "*If non-nil, is file name to use for explicitly requested inferior shell.")
  48.  
  49.  
  50. ;In loaddefs.el now.
  51. ;(defconst shell-prompt-pattern
  52. ;  "^[^#$%>]*[#$%>] *"
  53. ;  "*Regexp used by Newline command to match subshell prompts.
  54. ;Anything from beginning of line up to the end of what this pattern matches
  55. ;is deemed to be prompt, and is not reexecuted.")
  56.  
  57. (defun shell-mode ()
  58.   "Major mode for interacting with an inferior shell.
  59. Shell name is same as buffer name, sans the asterisks.
  60. Return at end of buffer sends line as input.
  61. Return not at end copies rest of line to end and sends it.
  62.  
  63. The following commands imitate the usual Unix interrupt and
  64. editing control characters:
  65. \\{shell-mode-map}
  66.  
  67. Entry to this mode calls the value of shell-mode-hook with no args,
  68. if that value is non-nil.
  69.  
  70. cd, pushd and popd commands given to the shell are watched
  71. by Emacs to keep this buffer's default directory
  72. the same as the shell's working directory.
  73. Variables shell-cd-regexp, shell-pushd-regexp and shell-popd-regexp
  74. are used to match these command names.
  75.  
  76. You can send text to the shell (or its subjobs) from other buffers
  77. using the commands process-send-region, process-send-string
  78. and lisp-send-defun."
  79.   (interactive)
  80.   (kill-all-local-variables)
  81.   (setq major-mode 'shell-mode)
  82.   (setq mode-name "Shell")
  83.   (setq mode-line-process '(": %s"))
  84.   (use-local-map shell-mode-map)
  85.   (make-local-variable 'shell-directory-stack)
  86.   (setq shell-directory-stack nil)
  87.   (make-local-variable 'last-input-start)
  88.   (setq last-input-start (make-marker))
  89.   (make-local-variable 'last-input-end)
  90.   (setq last-input-end (make-marker))
  91.   (run-hooks 'shell-mode-hook))
  92.  
  93. (if shell-mode-map
  94.     nil
  95.   (setq shell-mode-map (make-sparse-keymap))
  96.   (define-key shell-mode-map "\C-m" 'shell-send-input)
  97.   (define-key shell-mode-map "\C-c\C-d" 'shell-send-eof)
  98.   (define-key shell-mode-map "\C-c\C-u" 'kill-shell-input)
  99.   (define-key shell-mode-map "\C-c\C-w" 'backward-kill-word)
  100.   (define-key shell-mode-map "\C-c\C-c" 'interrupt-shell-subjob)
  101.   (define-key shell-mode-map "\C-c\C-z" 'stop-shell-subjob)
  102.   (define-key shell-mode-map "\C-c\C-\\" 'quit-shell-subjob)
  103.   (define-key shell-mode-map "\C-c\C-o" 'kill-output-from-shell)
  104.   (define-key shell-mode-map "\C-c\C-r" 'show-output-from-shell)
  105.   (define-key shell-mode-map "\C-c\C-y" 'copy-last-shell-input))
  106.  
  107.  
  108. (defvar explicit-csh-args
  109.   (if (eq system-type 'hpux)
  110.       ;; -T persuades HP's csh not to think it is smarter
  111.       ;; than us about what terminal modes to use.
  112.       '("-i" "-T")
  113.     '("-i"))
  114.   "Args passed to inferior shell by M-x shell, if the shell is csh.
  115. Value is a list of strings, which may be nil.")
  116.  
  117.  
  118. (defun shell ()
  119.   "Run an inferior shell, with I/O through buffer *shell*.
  120. If buffer exists but shell process is not running, make new shell.
  121. Program used comes from variable explicit-shell-file-name,
  122.  or (if that is nil) from the ESHELL environment variable,
  123.  or else from SHELL if there is no ESHELL.
  124. If a file ~/.emacs_SHELLNAME exists, it is given as initial input
  125.  (Note that this may lose due to a timing error if the shell
  126.   discards input when it starts up.)
  127. The buffer is put in shell-mode, giving commands for sending input
  128. and controlling the subjobs of the shell.  See shell-mode.
  129. See also variable shell-prompt-pattern.
  130.  
  131. The shell file name (sans directories) is used to make a symbol name
  132. such as `explicit-csh-arguments'.  If that symbol is a variable,
  133. its value is used as a list of arguments when invoking the shell.
  134. Otherwise, one argument `-i' is passed to the shell.
  135.  
  136. Note that many people's .cshrc files unconditionally clear the prompt.
  137. If yours does, you will probably want to change it."
  138.   (interactive)
  139.   (let* ((prog (or explicit-shell-file-name
  140.            (getenv "ESHELL")
  141.            (getenv "SHELL")
  142.            "/bin/sh"))             
  143.      (name (file-name-nondirectory prog)))
  144.     (switch-to-buffer
  145.      (apply 'make-shell "shell" prog
  146.         (if (file-exists-p (concat "~/.emacs_" name))
  147.         (concat "~/.emacs_" name))
  148.         (let ((symbol (intern-soft (concat "explicit-" name "-args"))))
  149.           (if (and symbol (boundp symbol))
  150.           (symbol-value symbol)
  151.         '("-i")))))))
  152.  
  153. (defun make-shell (name program &optional startfile &rest switches)
  154.   (let ((buffer (get-buffer-create (concat "*" name "*")))
  155.     proc status size)
  156.     (setq proc (get-buffer-process buffer))
  157.     (if proc (setq status (process-status proc)))
  158.     (save-excursion
  159.       (set-buffer buffer)
  160.       ;;    (setq size (buffer-size))
  161.       (if (memq status '(run stop))
  162.       nil
  163.     (if proc (delete-process proc))
  164.     (setq proc (apply 'start-process name buffer
  165.               (concat exec-directory "env")
  166.               (format "TERMCAP=emacs:co#%d:tc=unknown:"
  167.                   (screen-width))
  168.               "TERM=emacs"
  169.               "EMACS=t"
  170.               "-"
  171.               (or program explicit-shell-file-name
  172.                   (getenv "ESHELL")
  173.                   (getenv "SHELL")
  174.                   "/bin/sh")
  175.               switches))
  176.     (cond (startfile
  177.            ;;This is guaranteed to wait long enough
  178.            ;;but has bad results if the shell does not prompt at all
  179.            ;;         (while (= size (buffer-size))
  180.            ;;           (sleep-for 1))
  181.            ;;I hope 1 second is enough!
  182.            (sleep-for 1)
  183.            (goto-char (point-max))
  184.            (insert-file-contents startfile)
  185.            (setq startfile (buffer-substring (point) (point-max)))
  186.            (delete-region (point) (point-max))
  187.            (process-send-string proc startfile)))
  188.     (setq name (process-name proc)))
  189.       (goto-char (point-max))
  190.       (set-marker (process-mark proc) (point))
  191.       (shell-mode))
  192.     buffer))
  193.  
  194. (defvar shell-set-directory-error-hook 'ignore
  195.   "Function called with no arguments when shell-send-input
  196. recognizes a change-directory command but gets an error
  197. trying to change Emacs's default directory.")
  198.  
  199. (defun shell-send-input ()
  200.   "Send input to subshell.
  201. At end of buffer, sends all text after last output
  202.  as input to the subshell, including a newline inserted at the end.
  203. When not at end, copies current line to the end of the buffer and sends it,
  204. after first attempting to discard any prompt at the beginning of the line
  205. by matching the regexp that is the value of shell-prompt-pattern if possible.
  206. This regexp should start with \"^\"."
  207.   (interactive)
  208.   (or (get-buffer-process (current-buffer))
  209.       (error "Current buffer has no process"))
  210.   (end-of-line)
  211.   (if (eobp)
  212.       (progn
  213.     (move-marker last-input-start
  214.              (process-mark (get-buffer-process (current-buffer))))
  215.     (insert ?\n)
  216.     (move-marker last-input-end (point)))
  217.     (beginning-of-line)
  218.     ;; Exclude the shell prompt, if any.
  219.     (re-search-forward shell-prompt-pattern
  220.                (save-excursion (end-of-line) (point))
  221.                t)
  222.     (let ((copy (buffer-substring (point)
  223.                   (progn (forward-line 1) (point)))))
  224.       (goto-char (point-max))
  225.       (move-marker last-input-start (point))
  226.       (insert copy)
  227.       (move-marker last-input-end (point))))
  228.   ;; Even if we get an error trying to hack the working directory,
  229.   ;; still send the input to the subshell.
  230.   (condition-case ()
  231.       (save-excursion
  232.     (goto-char last-input-start)
  233.     (shell-set-directory))
  234.     (error (funcall shell-set-directory-error-hook)))
  235.   (let ((process (get-buffer-process (current-buffer))))
  236.     (process-send-region process last-input-start last-input-end)
  237.     (set-marker (process-mark process) (point))))
  238.  
  239. ;;;  If this code changes (shell-send-input and shell-set-directory),
  240. ;;;  the customization tutorial in
  241. ;;;  info/customizing-tutorial must also change, since it explains this
  242. ;;;  code.  Please let marick@gswd-vms.arpa know of any changes you
  243. ;;;  make. 
  244.  
  245.  
  246. (defun shell-set-directory ()
  247.   (cond ((and (looking-at shell-popd-regexp)
  248.           (memq (char-after (match-end 0)) '(?\; ?\n)))
  249.      (if shell-directory-stack
  250.          (progn
  251.            (cd (car shell-directory-stack))
  252.            (setq shell-directory-stack (cdr shell-directory-stack)))))
  253.     ((looking-at shell-pushd-regexp)
  254.      (cond ((memq (char-after (match-end 0)) '(?\; ?\n))
  255.         (if shell-directory-stack
  256.             (let ((old default-directory))
  257.               (cd (car shell-directory-stack))
  258.               (setq shell-directory-stack
  259.                 (cons old (cdr shell-directory-stack))))))
  260.            ((memq (char-after (match-end 0)) '(?\  ?\t))
  261.         (let (dir)
  262.           (skip-chars-forward "^ ")
  263.           (skip-chars-forward " \t")
  264.           (if (file-directory-p
  265.             (setq dir
  266.                   (expand-file-name
  267.                 (substitute-in-file-name
  268.                   (buffer-substring
  269.                     (point)
  270.                     (progn
  271.                       (skip-chars-forward "^\n \t;")
  272.                       (point)))))))
  273.               (progn
  274.             (setq shell-directory-stack
  275.                   (cons default-directory shell-directory-stack))
  276.             (cd dir)))))))
  277.     ((looking-at shell-cd-regexp)
  278.      (cond ((memq (char-after (match-end 0)) '(?\; ?\n))
  279.         (cd (getenv "HOME")))
  280.            ((memq (char-after (match-end 0)) '(?\  ?\t))
  281.         (let (dir)
  282.           (forward-char 3)
  283.           (skip-chars-forward " \t")
  284.           (if (file-directory-p
  285.             (setq dir
  286.                   (expand-file-name
  287.                 (substitute-in-file-name
  288.                   (buffer-substring
  289.                     (point)
  290.                     (progn
  291.                       (skip-chars-forward "^\n \t;")
  292.                       (point)))))))
  293.               (cd dir))))))))
  294.   
  295. (defun shell-send-eof ()
  296.   "Send eof to subshell (or to the program running under it)."
  297.   (interactive)
  298.   (process-send-eof))
  299.  
  300. (defun kill-output-from-shell ()
  301.   "Kill all output from shell since last input."
  302.   (interactive)
  303.   (goto-char (point-max))
  304.   (beginning-of-line)
  305.   (kill-region last-input-end (point))
  306.   (insert "*** output flushed ***\n")
  307.   (goto-char (point-max)))
  308.  
  309. (defun show-output-from-shell ()
  310.   "Display start of this batch of shell output at top of window.
  311. Also put cursor there."
  312.   (interactive)
  313.   (set-window-start (selected-window) last-input-end)
  314.   (goto-char last-input-end))
  315.  
  316. (defun copy-last-shell-input ()
  317.   "Copy previous shell input, sans newline, and insert before point."
  318.   (interactive)
  319.   (insert (buffer-substring last-input-end last-input-start))
  320.   (delete-char -1))
  321.  
  322. (defun interrupt-shell-subjob ()
  323.   "Interrupt this shell's current subjob."
  324.   (interactive)
  325.   (interrupt-process nil t))
  326.  
  327. (defun kill-shell-subjob ()
  328.   "Send kill signal to this shell's current subjob."
  329.   (interactive)
  330.   (kill-process nil t))
  331.  
  332. (defun quit-shell-subjob ()
  333.   "Send quit signal to this shell's current subjob."
  334.   (interactive)
  335.   (quit-process nil t))
  336.  
  337. (defun stop-shell-subjob ()
  338.   "Stop this shell's current subjob."
  339.   (interactive)
  340.   (stop-process nil t))
  341.  
  342. (defun kill-shell-input ()
  343.   "Kill all text since last stuff output by the shell or its subjobs."
  344.   (interactive)
  345.   (kill-region (process-mark (get-buffer-process (current-buffer)))
  346.            (point)))
  347.